package com.zhangmeng.tools.json2Pojo;

import com.sun.codemodel.CodeWriter;
import com.sun.codemodel.JPackage;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

/**
 * @author : 芊芊墨客
 * @version : 1.0
 * @date : 2023-04-21 09:21
 *
 *  * 源代码链接地址: https://blog.csdn.net/weixin_39651356/article/details/127155659
 */
public class MyCodeWriter extends CodeWriter {


    private final PrintStream out;

    /**
     * @param os
     *      This stream will be closed at the end of the code generation.
     */
    public MyCodeWriter( OutputStream os ) {
        this.out = new PrintStream(os);;
    }

    @Override
    public OutputStream openBinary(JPackage pkg, String fileName) throws IOException {
        String pkgName = pkg.name();
        if(pkgName.length()!=0)     pkgName += '.';
        return new FilterOutputStream(out) {
            public void close() {
                // don't let this stream close
            }
        };
    }

    @Override
    public void close() throws IOException {
        out.close();
    }
}